package cn.com.cis.web.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import cn.com.cis.domain.Connection;
import cn.com.cis.plugins.mybatis.PageInfo;
import cn.com.cis.service.ConnectionService;
import cn.com.cis.utils.DatabaseHelper;

@Controller
@RequestMapping(value = "/connection")
public class ConnectionController {
	
	@Autowired
	private ConnectionService connService;
    
    private void setNavigateBar(ModelAndView mv){
    	mv.addObject("icon", "icon-list");
        mv.addObject("title", "数据源管理");
    }

    @RequiresPermissions("配置管理:数据源管理:查看数据源列表")
    @RequestMapping(value = "/list")
    public ModelAndView list(
    		@RequestParam(value = "page", defaultValue = "1", required = false) int page,
            @RequestParam(value = "pageSize", defaultValue = "5", required = false) int pageSize) {
        ModelAndView mav = new ModelAndView("connection/list");
        setNavigateBar(mav);
        
        List<Connection> connections = connService.selectAllConnections(page, pageSize);
        PageInfo<Connection> pageInfo = new PageInfo<Connection>(connections, pageSize);
        
        mav.addObject("page", page);
        mav.addObject("connectionList", connections);
        mav.addObject("pageInfo", pageInfo);
        
        return mav;
    }

    @RequiresPermissions("配置管理:数据源管理:添加数据源")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("connection/add");
        setNavigateBar(mav);
        
        return mav;
    }

    @RequiresPermissions("配置管理:数据源管理:添加数据源")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ModelAndView save(Connection bean, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/connection/list");
        setNavigateBar(mav);
        
        // 去掉前端提交格式里的下划线
        bean.setHostIp(bean.getHostIp().replace("_", ""));
        
        connService.insertConnection(bean);
        
        return mav;
    }

    @RequiresPermissions("配置管理:数据源管理:修改数据源")
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView editView(int id) {
        ModelAndView mav = new ModelAndView("connection/edit");
        setNavigateBar(mav);
        
        Connection connection = connService.selectConnection(id);
        mav.addObject("connection", connection);
        
        return mav;
    }

    @RequiresPermissions("配置管理:数据源管理:修改数据源")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelAndView update(Connection bean,RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/connection/list");
        setNavigateBar(mav);
        
        // 去掉前端提交格式里的下划线
        bean.setHostIp(bean.getHostIp().replace("_", ""));
        connService.updateConnection(bean);
        
        return mav;
    }

    @RequiresPermissions("配置管理:数据源管理:删除数据源")
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public ModelAndView del(int id, RedirectAttributesModelMap modelMap) {
        ModelAndView mav = new ModelAndView("redirect:/connection/list");
        setNavigateBar(mav);
        
        connService.deleteConnection(id);
        
        return mav;
    }

    @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
	public String test(int id) {

		Connection connection = connService.selectConnection(id);

		boolean isAvaliable = DatabaseHelper.isConnectionAvaliable(connection.getDatabaseType(), connection.getHostIp(), connection.getPort(), connection.getDatabaseName(), connection.getUsername(),
				connection.getPassword());
		
		if (isAvaliable) {
			return "success";
		}

		return "failed";
	}

}
